#!/usr/bin/env python3

# 
# Run the test, compare results against the benchmark
#


from __future__ import print_function

try:
  from builtins import str
except:
  pass

vars = ['a', 'b', 'c', 'd']  # Variables to compare
tol = 1e-10                  # Absolute tolerance

from boututils.run_wrapper import shell, shell_safe, launch_safe
from boutdata.collect import collect
import numpy as np
from sys import stdout, exit



print("Making FieldFactory test")
shell_safe("make > make.log")

# Read benchmark values
print("Reading benchmark data")
bmk = {}
for v in vars:
  bmk[v] = collect(v, path="data", prefix="benchmark", info=False)

print("Running FieldFactory test")
success = True

for nproc in [1,2,4]:
  cmd = "./test_fieldfactory"
  
  shell("rm data/BOUT.dmp.*.nc")

  print("   %d processor...." % (nproc))
  s, out = launch_safe(cmd, nproc=nproc, pipe=True)
  with open("run.log."+str(nproc), "w") as f:
    f.write(out)

   # Collect output data
  for v in vars:
    stdout.write("      Checking variable "+v+" ... ")
    result = collect(v, path="data", info=False)
    # Compare benchmark and output
    if np.shape(bmk[v]) != np.shape(result):
      print("Fail, wrong shape")
      success = False
    diff =  np.max(np.abs(bmk[v] - result))
    if diff > tol:
      print("Fail, maximum difference = "+str(diff))
      success = False
    else:
      print("Pass")

if success:
  print(" => All FieldFactory tests passed")
  exit(0)
else:
  print(" => Some failed tests")
  exit(1)
